$(function () {
    //抽取变量
    var salary_form = $("#salary_form");
    var salary_dialog = $("#salary_dialog");
    var salary1_form = $("#salary1_form");
    var salary1_dialog = $("#salary1_dialog");
    var salary_dataGrid = $("#salary_dataGrid");

    //初始化datagrid
    salary_dataGrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/salary/query.do",
        pagination: true,
        singleSelect: true,
        toolbar: "#sal_toolbar",
        columns: [[
            {field: 'emp', title: '姓名', width: 100, align: 'center',formatter:function(value, row, index) {
                    return value ? value.username : "";
                }},
            {field: 'dept', title: '部门', width: 100, align: 'center',formatter:function(value, row, index) {
                    return value ? value.name : "";
                }},
            {field:'tel', title:'电话', width: 100, align: 'center'},
            {field:'email', title:'邮箱', width: 100, align: 'center'},
            {field:'salary', title:'基本工资', width: 100, align: 'center'},
            {field:'bonus', title:'奖金', width: 100, align: 'center'},
            {field:'date', title:'日期', width: 100, align: 'center'},
            {field:'totalSalary', title:'总薪资', width: 100, align: 'center'},

        ]],

    });

    //初始化dialog
    salary_dialog.dialog({
        width: 300,
        height: 500,
        buttons: "#sche_btns",
        closed: true
    });
    //初始化dialog
    salary1_dialog.dialog({
        width: 300,
        height: 520,
        buttons: "#sche1_btns",
        closed: true
    });
    var methodObj = {
        add: function () {
            salary1_form.form("clear");
            salary1_dialog.dialog("setTitle", "新建员工薪资");
            salary1_dialog.dialog("open");
        },
        //查看任务明细
        look: function () {
            var row = salary_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中查看的数据', 'info');
                return;
            }
            //处理部门数据
            row["emp.id"] = row.emp.id;
            row["dept.id"] = row.dept.id;
            salary_form.form("load", row);
            salary_dialog.dialog("setTitle", "编辑")
            salary_dialog.dialog("open");
        },
        //高级查询
        searchForm:function() {
            var keyword = $("input[name='keyword']").val();
            var deptId = $(".dept_combobox").combobox('getValue');
            var beginDate = $("input[name='beginDate']").val();
            var endDate = $("input[name='endDate']").val();

            salary_dataGrid.datagrid("load", {
                keyword: keyword,
                deptId: deptId,
                beginDate: beginDate,
                endDate: endDate
            });
        },
        //新增
        save: function () {
            salary1_form.form("submit", {
                url: "/salary/saveOrUpdate.do",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            salary1_dialog.dialog("close");
                            salary_dataGrid.datagrid("load");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info");
                    }
                }
            })
        },
        //取消
        cancel: function () {
            salary1_dialog.dialog("close");
        },
    }
    //所有a标签的点击事件对应的方法都交给methodObject对象来管理,调用的时候只需要获取对应的cmd质量,然后通过method对象来调用即可
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

});


